package com.example.service.impl;

import com.example.entity.Staffs;
import com.example.mapper.StaffsMapper;
import com.example.result.CommonResult;
import com.example.service.StaffsService;
import com.example.utils.Md5Utils;
import com.example.utils.MyOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @project: smart-property
 * @description: 员工管理 service 实现层
 * @author: cjq
 * @create: 2022-11-16 15:52
 **/
@Service
public class StaffsServiceImpl implements StaffsService {
    @Autowired
    StaffsMapper staffsMapper;

    /**
     * 判断员工姓名是否有重复
     * @param staffName
     * @return
     */
    @Override
    public CommonResult listStaffName(String staffName) {
        Staffs staffs = staffsMapper.listStaffName(staffName);
        if (!StringUtils.isEmpty(staffs)){
            return CommonResult.ok(staffs);
        }
        return CommonResult.fail();
    }

    /**
     * 添加/修改个人信息
     * @param file
     * @param staffs
     * @return
     */
    @Override
    public int saveStaffsInfo(MultipartFile file, Staffs staffs) {
        int i=0;
        if (file != null){
            MyOSSUtil myOSSUtil = new MyOSSUtil();
            String s = myOSSUtil.uploadFile(file);
            if (staffs.getId() == null){
                staffs.setStatus(0);
                staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
                staffs.setCreateTime(new Date());
                staffs.setStaffImageUrl(s);
                i = staffsMapper.addStaffsInfo(staffs);
            }else {
                staffs.setStatus(0);
                if (!StringUtils.isEmpty(staffs.getPassword())){
                    staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
                }
                staffs.setCreateTime(new Date());
                staffs.setStaffImageUrl(s);
                i = staffsMapper.updateStaffsInfo(staffs);
            }
        }else {
            if (staffs.getId() == null){
                staffs.setStatus(0);
                staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
                staffs.setCreateTime(new Date());
                i = staffsMapper.addStaffsInfo(staffs);
            }else {
                staffs.setStatus(0);
                if (!StringUtils.isEmpty(staffs.getPassword())){
                    staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
                }
                staffs.setCreateTime(new Date());
                i = staffsMapper.updateStaffsInfo(staffs);
            }
        }
        return i;
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public CommonResult listStaffsInfo(Integer id) {
        return CommonResult.ok(staffsMapper.listStaffsInfo(id));
    }

    /**
     * 重置密码为123456
     * @param id
     * @return
     */
    @Override
    public int resetPassword(Integer id) {
        String password = Md5Utils.getMd5("123456");
        return staffsMapper.resetPassword(id,password);
    }
}
